//
//  ZLWRadioPlayerController.m
//  EPA
//
//  Created by lanou3g on 16/4/21.
//  Copyright © 2016年 zlw. All rights reserved.
//

#import "ZLWRadioPlayerController.h"
#import "ZLWAVManager.h"
#import "ZLWRadioListModel.h"
@interface ZLWRadioPlayerController ()
/**
 *  标题
 */
@property (weak, nonatomic) IBOutlet UILabel *TitleButton;
/**
 *  封面
 */
@property (weak, nonatomic) IBOutlet UIImageView *coverImageView;
/**
 *  当前时长
 */
@property (weak, nonatomic) IBOutlet UILabel *currentLable;
/**
 *  进度
 */
@property (weak, nonatomic) IBOutlet UISlider *rateSlider;
/**
 *  总时长
 */
@property (weak, nonatomic) IBOutlet UILabel *TotalLable;
/**
 *  计时器
 */
@property (nonatomic, strong)NSTimer *playTimer;
/**
 *  上一曲
 */

@property (weak, nonatomic) IBOutlet UIButton *PreButton;
@end

@implementation ZLWRadioPlayerController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    ZLWRadioListModel *model = self.radioArray[self.currentIndex];
    self.navigationController.navigationBar.translucent = NO;
    self.TitleButton.text = model.title;
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    
    
    
    [ZLWAVManager shareManager].currentIndex = self.currentIndex;
    NSMutableArray *itemArray = [NSMutableArray new];
    for (ZLWRadioListModel *model in self.radioArray) {
        NSURL *musicUrl = [NSURL URLWithString:model.playInModel.musicUrl];
        AVPlayerItem *item = [AVPlayerItem playerItemWithURL:musicUrl];
        [itemArray addObject:item];
    }
    
    //AVPlayerItem数组
    [ZLWAVManager shareManager].itemArray = itemArray;
    [[ZLWAVManager shareManager] Play];
    
    
    
    //创建 timer
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
}
-(void)timeAction
{
    //当前时间
    float current = 0;
    if ([ZLWAVManager shareManager].avplayer.currentTime.timescale > 0) {
        current = [ZLWAVManager shareManager].avplayer.currentTime.value / [ZLWAVManager shareManager].avplayer.currentTime.timescale;
    }
    NSString *currentStr = [NSString stringWithFormat:@"%02ld:%2ld",(NSInteger)current/60 ,(NSInteger)current%60];
    self.currentLable.text = currentStr;
    
    //总时间
    float total = 0;
    if ([ZLWAVManager shareManager].avplayer.currentItem.duration.timescale > 0) {
        total = [ZLWAVManager shareManager].avplayer.currentItem.duration.value / [ZLWAVManager shareManager].avplayer.currentItem.duration.timescale;
    }
    NSString *totalStr = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)total/60,(NSInteger)total%60];
    self.TotalLable.text = totalStr;
    if (total > 0) {
        float rate = current /total;
        self.rateSlider.value = rate;
    }
    
}
- (IBAction)didCllickPreviosButton:(UIButton *)sender {
    [[ZLWAVManager shareManager] previous];
    //图片和标题
    self.currentIndex--;
    
    if (self.currentIndex < 0) {
        self.currentIndex = self.radioArray.count - 1;
    }
    ZLWRadioListModel *model = self.radioArray[self.currentIndex];
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    self.TitleButton.text = model.title;
 
}
- (IBAction)didClickPlayButton:(UIButton *)sender {
    if ([ZLWAVManager shareManager].avplayer.rate == 0) {
        [[ZLWAVManager shareManager] Play];
        [(UIButton *)sender setImage:[UIImage imageNamed:@"播放(1).png"] forState:(UIControlStateNormal)];
    }else
    {
        [[ZLWAVManager shareManager] Pause];
        [(UIButton *)sender setImage:[UIImage imageNamed:@"播放.png"] forState:(UIControlStateNormal)];
        
    }
}
- (IBAction)didClilckNextButton:(UIButton *)sender {
    [[ZLWAVManager shareManager] Next];
    //图片标题
    self.currentIndex++;
    if (self.currentIndex >= self.radioArray.count) {
        self.currentIndex = 0;
    }
    ZLWRadioListModel *model = self.radioArray[self.currentIndex];
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    self.TitleButton.text = model.title;
    //    [self.PreButton setImage:[UIImage imageNamed:@"播放(1).png"] forState:(UIControlStateNormal)];
}
- (IBAction)didChangeRateAction:(UISlider *)sender {
    CMTime changTime= CMTimeMake(sender.value * [ZLWAVManager shareManager].avplayer.currentItem.duration.value, [ZLWAVManager shareManager].avplayer.currentItem.duration.timescale);
    //seekTotIme
    [[ZLWAVManager shareManager] SeekToTime:changTime];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
